package com.guigu.aclservice.controller;

import com.guigu.aclservice.entity.User;
import com.guigu.aclservice.service.UserService;
import com.guigu.commonutils.JwtUtils;
import com.guigu.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;

/**
 * @Author: janecraft
 * @Date: 2022/6/1
 * @Description: com.guigu.aclservice.controller
 * @Version： 1.0
 */
@RestController
@RequestMapping("/aclservice/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R login(@RequestBody User user) {
        // 调用业务层的身份认证的方法
        String token = userService.canLogin(user.getUsername(), user.getPassword());
        return R.ok().data("token", token);
    }

    // 获取认证成功的用户信息
    @GetMapping("/info")
    public R info(HttpServletRequest request) {
        // 获取认证成功的用户id
        String id = JwtUtils.getMemberIdByJwtToken(request);

        // 根据用户id查询数据库或者缓存获取需要的业务数据


        // 模拟业务操作
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("roles", Arrays.asList("系统管理员","后台管理员"));
        hashMap.put("name", "小阿三");
        hashMap.put("avatar", "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcrawl.ws.126.net%2Fimg%2F238308a1210b9ee800950c8756bff2bc.jpg&refer=http%3A%2F%2Fcrawl.ws.126.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1656726365&t=9d2781878f1a3da2a373c6edadc86542");
        return R.ok().data(hashMap);
    }
}
